Ext.ns("CarApplyView");
CarApplyView = Ext
		.extend(
				Ext.Panel,
				{
					searchPanel : null,
					gridPanel : null,
					store : null,
					topbar : null,
					constructor : function(a) {
						Ext.applyIf(this, a);
						this.initUIComponents();
						CarApplyView.superclass.constructor.call(this, {
							id : "CarApplyView",
							title : "车辆申请列表",
							iconCls : "menu-car_apply",
							region : "center",
							layout : "border",
							items : [ this.searchPanel, this.gridPanel ]
						});
					},
					initUIComponents : function() {
						this.searchPanel = new Ext.FormPanel(
								{
									id : "CarApplySearchForm",
									height : 40,
									region : "north",
									frame : false,
									border : false,
									layout : "hbox",
									layoutConfig : {
										padding : "5",
										align : "middle"
									},
									defaults : {
										xtype : "label",
										margins : {
											top : 0,
											right : 4,
											bottom : 4,
											left : 4
										}
									},
									items : [
											{
												text : "请输入查询条件:"
											},
											{
												text : "车牌号"
											},
											{
												xtype : "textfield",
												name : "Q_car.carNo_S_LK"
											},
											{
												text : "审批状态"
											},
											{
												xtype : "textfield",
												hiddenName : "Q_approvalStatus_SN_EQ",
												xtype : "combo",
												mode : "local",
												allowBlank : false,
												editable : false,
												triggerAction : "all",
												store : [ [ "0", "未审批" ],
														[ "1", "已审批" ] ]
											},
											{
												xtype : "button",
												text : "查询",
												iconCls : "search",
												handler : function() {
													var c = Ext
															.getCmp("CarApplySearchForm");
													var d = Ext
															.getCmp("CarApplyGrid");
													if (c.getForm().isValid()) {
														c
																.getForm()
																.submit(
																		{
																			waitMsg : "正在提交查询",
																			url : __ctxPath
																					+ "/admin/listCarApply.do",
																			success : function(
																					f,
																					g) {
																				var e = Ext.util.JSON
																						.decode(g.response.responseText);
																				d
																						.getStore()
																						.loadData(
																								e);
																			}
																		});
													}
												}
											} ]
								});
						this.store = new Ext.data.Store( {
							proxy : new Ext.data.HttpProxy( {
								url : __ctxPath + "/admin/listCarApply.do"
							}),
							reader : new Ext.data.JsonReader( {
								root : "result",
								totalProperty : "totalCounts",
								id : "id",
								fields : [ {
									name : "applyId",
									type : "int"
								}, {
									name : "carNo",
									mapping : "car.carNo"
								}, "department", "userFullname", "applyDate",
										"reason", "startTime", "endTime",
										"proposer", "mileage", "oilUse",
										"notes", "approvalStatus" ]
							}),
							remoteSort : true
						});
						this.store.setDefaultSort("applyId", "desc");
						this.store.load( {
							params : {
								start : 0,
								limit : 25
							}
						});
						var b = new Ext.grid.CheckboxSelectionModel();
						var a = new Ext.grid.ColumnModel(
								{
									columns : [
											b,
											new Ext.grid.RowNumberer(),
											{
												header : "applyId",
												dataIndex : "applyId",
												hidden : true
											},
											{
												header : "车辆车牌号",
												dataIndex : "carNo"
											},
											{
												header : "用车部门",
												dataIndex : "department"
											},
											{
												header : "用车人",
												dataIndex : "userFullname"
											},
											{
												header : "申请日期",
												dataIndex : "applyDate",
												renderer : function(c) {
													return c.substring(0, 10);
												}
											},
											{
												header : "原因",
												dataIndex : "reason"
											},
											{
												header : "开始时间",
												dataIndex : "startTime"
											},
											{
												header : "结束时间",
												dataIndex : "endTime"
											},
											{
												header : "申请人",
												dataIndex : "proposer"
											},
											{
												header : "审批状态",
												dataIndex : "approvalStatus",
												renderer : function(c) {
													if (c == "0") {
														return "未审批";
													} else {
														if (c == "1") {
															return '<font color="green">通过审批</font>';
														} else {
															return '<font color="red">未通过审批</font>';
														}
													}
												}
											},
											{
												header : "管理",
												dataIndex : "applyId",
												width : 80,
												sortable : false,
												renderer : function(g, f, d, j,
														e) {
													var i = d.data.applyId;
													var c = d.data.approvalStatus;
													var h = "";
													if (isGranted("__CarApplyDel")) {
														h = '<button title="删除" value=" " class="btn-del" onclick="CarApplyView.remove(' + i + ')">&nbsp;</button>';
													}
													if (isGranted("_CarApplyEdit")) {
														if (c == 0) {
															h += '&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="CarApplyView.edit(' + i + ')">&nbsp;</button>';
														}
													}
													if (isGranted("_CarApplyCheck")) {
														if (c == 0) {
															h += '&nbsp;<button title="审批" value=" " class="btn-ok" onclick="CarApplyView.check(' + i + ')">&nbsp;</button>';
														}
													}
													return h;
												}
											} ],
									defaults : {
										sortable : true,
										menuDisabled : false,
										width : 100
									}
								});
						this.topbar = new Ext.Toolbar( {
							id : "CarApplyFootBar",
							height : 30,
							bodyStyle : "text-align:left",
							items : []
						});
						if (isGranted("_CarApplyAdd")) {
							this.topbar.add(new Ext.Button( {
								iconCls : "btn-carapply-add",
								text : "添加车辆申请单",
								handler : function() {
									new CarApplyForm().show();
								}
							}));
						}
						if (isGranted("_CarApplyEdit")) {
							this.topbar.add(new Ext.Button( {
								iconCls : "btn-carapply-del",
								text : "删除车辆申请单",
								handler : function() {
									var e = Ext.getCmp("CarApplyGrid");
									var c = e.getSelectionModel()
											.getSelections();
									if (c.length == 0) {
										Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
										return;
									}
									var f = Array();
									for ( var d = 0; d < c.length; d++) {
										f.push(c[d].data.applyId);
									}
									CarApplyView.remove(f);
								}
							}));
						}
						this.gridPanel = new Ext.grid.GridPanel( {
							id : "CarApplyGrid",
							tbar : this.topbar,
							region : "center",
							store : this.store,
							trackMouseOver : true,
							disableSelection : false,
							loadMask : true,
							autoHeight : true,
							cm : a,
							sm : b,
							viewConfig : {
								forceFit : true,
								enableRowBody : false,
								showPreview : false
							},
							bbar : new Ext.PagingToolbar( {
								pageSize : 25,
								store : this.store,
								displayInfo : true,
								displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
								emptyMsg : "当前没有记录"
							})
						});
						this.gridPanel.addListener("rowdblclick", function(d,
								c, f) {
							d.getSelectionModel().each(function(e) {
								if (isGranted("_CarApplyEdit")) {
									if (e.data.approvalStatus == 0) {
										CarApplyView.edit(e.data.applyId);
									}
								}
							});
						});
					}
				});
CarApplyView.remove = function(b) {
	var a = Ext.getCmp("CarApplyGrid");
	Ext.Msg.confirm("信息确认", "您确认要删除该记录吗？", function(c) {
		if (c == "yes") {
			Ext.Ajax.request( {
				url : __ctxPath + "/admin/multiDelCarApply.do",
				params : {
					ids : b
				},
				method : "post",
				success : function() {
					Ext.ux.Toast.msg("信息提示", "成功删除所选记录！");
					a.getStore().reload( {
						params : {
							start : 0,
							limit : 25
						}
					});
				}
			});
		}
	});
};
CarApplyView.edit = function(a) {
	new CarApplyForm( {
		applyId : a
	}).show();
};
CarApplyView.check = function(a) {
	new CarApplyCheck( {
		applyId : a
	}).show();
};